package yt.kpool.activity.home;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.adapter.PoolCoinAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.PoolCoinBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;


/**
 * 主流币
 */
public class MainstreamCurrencyAc  extends AppCompatActivity {
    @BindView(R.id.main_recycler)
    RecyclerView mRecyclerView;
    private LoadingDialog dialog;
    private PoolCoinAdapter poolCoinAdapter;
    private List<PoolCoinBean.DataBean.CoinListBean> listData;
    private SPUtil spUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainstream);
        ButterKnife.bind(this);
        initTitle();
        init();
        if(NetWorkUtil.checkNet(MainstreamCurrencyAc.this))
        {
            getNetData();
        }
        else
        {

            ToastUtil.showToast(MainstreamCurrencyAc.this,getResources().getString(R.string.internet_error));
        }

    }

    private void init()
    {
        if (spUtil == null)
            spUtil = new SPUtil(this, "fileName");
        dialog = new LoadingDialog(this, 0);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        poolCoinAdapter = new PoolCoinAdapter(this,listData);
        mRecyclerView.setAdapter(poolCoinAdapter);

    }
    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("主流币");

    }

    /**
     * 获取首页网络数据
     */
    private void getNetData()
    {
        dialog.show();
        NetHelperNew.PoolCoinList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                PoolCoinBean poolCoinBean = MyGson.getInstance().fromJson(data, PoolCoinBean.class);
                     if(poolCoinBean.getType()!=1)
                     {
                         Toast.makeText(MainstreamCurrencyAc.this,poolCoinBean.getContent(),Toast.LENGTH_SHORT).show();
                     }
                     else
                     {
                         listData=poolCoinBean.getData().getCoinList();
                         poolCoinAdapter.setNewData(listData);
                         poolCoinAdapter.setExchange(poolCoinBean.getData().getExchange());
                         if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
                         {
                             poolCoinAdapter.setKind(1,0.0);


                         }
                         else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
                         {
                             poolCoinAdapter.setKind(2,0.0);


                         }
                         poolCoinAdapter.notifyDataSetChanged();
                     }

                    dialog.dismiss();

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(MainstreamCurrencyAc.this,msg);
            }
        });

    }



}
